package com.sky.common.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.business.sys.domain.CameraConfig;
import com.sky.business.sys.domain.SystemConfig;
import com.sky.business.sys.event.EdgeHeartbeatEvent;
import com.sky.business.sys.service.CameraConfigService;
import com.sky.business.sys.service.SystemConfigService;
import com.sky.business.sys.util.MQTTClientUtils;
import com.sky.common.utils.GeoCentroidCalculator;
import com.sky.common.utils.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.SimpleTimeZone;
import java.util.stream.Collectors;

/**
 * 边缘中心心跳定时任务
 */
@Slf4j
@Component
public class EdgeHeartbeatTask {

    @Autowired
    private CameraConfigService cameraConfigService;

    @Autowired
    private SystemConfigService systemConfigService;

    public static final ObjectMapper objectMapper = new ObjectMapper().setTimeZone(SimpleTimeZone.getTimeZone("GMT+8"));

    /**
     * 把设备经纬高转换为点位
     * @param cameraConfig 设备
     * @return 点位
     */
    public static GeoCentroidCalculator.Point convert(CameraConfig cameraConfig) {
        GeoCentroidCalculator.Point point = new GeoCentroidCalculator.Point();
        point.setLongitude(NumberUtil.parseDouble(cameraConfig.getCameraLongitude()));
        point.setLatitude(NumberUtil.parseDouble(cameraConfig.getCameraLatitude()));
        point.setAltitude(NumberUtil.parseDouble(cameraConfig.getCameraAltitude()));
        return point;
    }

    @Async("edgeHeartbeatThreadPoolTaskExecutor")
    @Scheduled(fixedRate = 1000)
    public void task() {
        try {
            log.debug("边缘中心上报心跳");
            List<CameraConfig> cameraConfigList = cameraConfigService.list();
            if (cameraConfigList.size() == 0) {
                return;
            }
            SystemConfig systemConfig = systemConfigService.getSystemConfig();
            List<GeoCentroidCalculator.Point> locations = cameraConfigList.stream().map(EdgeHeartbeatTask::convert).collect(Collectors.toList());
            GeoCentroidCalculator.Point point = GeoCentroidCalculator.calculateCentroid(locations);
            BigDecimal longitude = NumberUtil.toDecimal(point.getLongitude(),6);
            BigDecimal latitude = NumberUtil.toDecimal(point.getLatitude(),6);
            BigDecimal altitude =  NumberUtil.toDecimal(point.getAltitude(),6);
            EdgeHeartbeatEvent event = new EdgeHeartbeatEvent(systemConfig.getEdgeId(), systemConfig.getEdgeIp(), systemConfig.getEdgeName(), longitude,latitude, altitude);
            String msg = objectMapper.writeValueAsString(event);
            //发送消息
            MQTTClientUtils.publish(MQTTClientUtils.CMCC_MQTT_CLIENT, systemConfig.getCmccReportingTopic(), 0, msg);
        } catch (Exception e) {
            log.error("发送边缘中心的心跳发生异常",e);
        }
    }
}
